package com.itheima.mobilesafe74.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


import com.itheima.mobilesafe74.R;
import com.itheima.mobilesafe74.db.dao.AppLockDao;
import com.itheima.mobilesafe74.db.domain.AppInfo;
import com.itheima.mobilesafe74.engine.AppInfoProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/4.
 */
public class AppLockActivity extends Activity {
    private ListView lv_lock;
    private ListView lv_unlock;
    private TextView tv_unlock;
    private TextView tv_lock;
    private LinearLayout ll_unlock;
    private LinearLayout ll_lock;
    private Button bt_unlock;
    private Button bt_lock;
    private List<AppInfo> mAppInfoList;
    private List<AppInfo> mLockList;
    private List<AppInfo> mUnLockList;
    private AppLockDao mDao;
    private MyAdapter mLockAdapter;
    private MyAdapter mUnLockAdapter;
    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            //6、接收到消息，填充已加锁和未加锁的数据适配器
            mLockAdapter=new MyAdapter(true);
            lv_lock.setAdapter(mLockAdapter);

            mUnLockAdapter=new MyAdapter(false);
            lv_unlock.setAdapter(mUnLockAdapter);
        }
    };
    private TranslateAnimation mTranslateAnimation;

    class MyAdapter extends BaseAdapter{
        private boolean islock;
        /**
         * @param islock 用于区分已加锁和未加锁应用的标识 true 已加锁的数据适配器 false未加锁数据适配器
         */
        public MyAdapter(boolean islock){
            this.islock=islock;
        }
        @Override
        public int getCount() {
            if (islock){
                tv_lock.setText("已加锁应用："+mLockList.size());
                return mLockList.size();
            }else {
                tv_unlock.setText("未加锁应用："+mUnLockList.size());
                return mUnLockList.size();
            }
        }

        @Override
        public AppInfo getItem(int position) {
            if (islock){
                return mLockList.get(position);
            }else {
                return mUnLockList.get(position);
            }

        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position,View convertView, ViewGroup parent) {
            ViewHolder holder=null;
            if (convertView==null){
                convertView=View.inflate(getApplicationContext(),R.layout.listview_islock_item,null);
                holder=new ViewHolder();
                holder.iv_icon= (ImageView) convertView.findViewById(R.id.iv_icon);
                holder.tv_name= (TextView) convertView.findViewById(R.id.tv_name);
                holder.iv_lock= (ImageView) convertView.findViewById(R.id.iv_lock);

                convertView.setTag(holder);
            }else {
                holder= (ViewHolder) convertView.getTag();
            }
            final AppInfo appInfo=getItem(position);
            //执行动画的view
            final View animationView=convertView;
            holder.iv_icon.setBackgroundDrawable(appInfo.icon);
            holder.tv_name.setText(appInfo.name);
            if (islock){
                holder.iv_lock.setBackgroundResource(R.mipmap.lock);
            }else {
                holder.iv_lock.setBackgroundResource(R.mipmap.unlock);
            }
            holder.iv_lock.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //添加动画效果
                    animationView.startAnimation(mTranslateAnimation);
                    //对动画的执行的过程做事件监听，监听到动画完成后，再去移除集合中的数据，操作数据库，刷新界面
                    mTranslateAnimation.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {

                        }

                        //动画执行结束调用的方法
                        @Override
                        public void onAnimationEnd(Animation animation) {
                            if (islock){
                                //已加锁--->未加锁过程
                                //1、已加锁集合删除一个，未加锁集合添加一个，对象就是getItem方法获取的对象
                                mLockList.remove(appInfo);
                                mUnLockList.add(appInfo);
                                //2、从已加锁的数据库中删除一条数据
                                mDao.delete(appInfo.packageName);
                                //3、刷新数据适配器
                                mLockAdapter.notifyDataSetChanged();
                            }else {
                                //未加锁--->已加锁过程
                                //1、已加锁集合添加一个，未加锁集合删除一个，对象就是getItem方法获取的对象
                                mUnLockList.remove(appInfo);
                                mLockList.add(appInfo);
                                //2、从已加锁的数据库中插入一条数据
                                mDao.insert(appInfo.packageName);
                                //3、刷新数据适配器
                                mUnLockAdapter.notifyDataSetChanged();
                            }
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });
                }
            });
            return convertView;
        }
    }

    static class ViewHolder{
        ImageView iv_icon;
        TextView tv_name;
        ImageView iv_lock;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_lock);

        initUI();

        initData();

        initAnimation();
    }

    /**
     * 初始化平移动画的方法
     */
    private void initAnimation() {
        mTranslateAnimation=new TranslateAnimation(
                Animation.RELATIVE_TO_SELF,0,
                Animation.RELATIVE_TO_SELF,1,
                Animation.RELATIVE_TO_SELF,0,
                Animation.RELATIVE_TO_SELF,0
        );
        mTranslateAnimation.setDuration(500);
    }

    /**
     * 区分已加锁和未加锁的集合
     */
    private void initData() {
        new Thread(){
            @Override
            public void run() {
                //1、获取手机中的所有应用
                mAppInfoList=AppInfoProvider.getAppInfoList(getApplicationContext());
                //2、区分已加锁应用和未加锁应用
                mLockList=new ArrayList<AppInfo>();
                mUnLockList=new ArrayList<AppInfo>();
                //3、获取数据库中已加锁应用包名的集合
                mDao=AppLockDao.getInstance(getApplicationContext());
                List<String> lockPackageList=mDao.findAll();
                for (AppInfo appInfo:mAppInfoList){
                    //4、循环到的民工的包名在数据库中，则说明是已加锁应用
                    if (lockPackageList.contains(appInfo.packageName)){
                        mLockList.add(appInfo);
                    }else {
                        mUnLockList.add(appInfo);
                    }
                }
                //5、告知主线程，可以使用维护的数据
                mHandler.sendEmptyMessage(0);
            }
        }.start();

    }

    private void initUI() {
        bt_unlock= (Button) findViewById(R.id.bt_unlock);
        bt_lock= (Button) findViewById(R.id.bt_lock);

        ll_unlock= (LinearLayout) findViewById(R.id.ll_unlock);
        ll_lock= (LinearLayout) findViewById(R.id.ll_lock);

        tv_unlock= (TextView) findViewById(R.id.tv_unlock);
        tv_lock= (TextView) findViewById(R.id.tv_lock);

        lv_unlock= (ListView) findViewById(R.id.lv_unlock);
        lv_lock= (ListView) findViewById(R.id.lv_lock);

        bt_unlock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //1、已加锁列表隐藏，未加锁列表显示
                ll_lock.setVisibility(View.GONE);
                ll_unlock.setVisibility(View.VISIBLE);
                //2、已加锁变成浅色图片，未加锁变成深色图片
                bt_unlock.setBackgroundResource(R.mipmap.tab_left_pressed);
                bt_lock.setBackgroundResource(R.mipmap.tab_right_default);

            }
        });

        bt_lock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //1、已加锁列表显示，未加锁列表隐藏
                ll_lock.setVisibility(View.VISIBLE);
                ll_unlock.setVisibility(View.GONE);
                //2、未加锁变成浅色图片，已加锁变成深色图片
                bt_unlock.setBackgroundResource(R.mipmap.tab_left_default);
                bt_lock.setBackgroundResource(R.mipmap.tab_right_pressed);

            }
        });
    }
}
